package com.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @author:hanke 2022/6/16
 * desc:
 */
public class StorageUtils {
    public static final String TEMP_PATH = "/temp/";
    public static final String SHORT_VIDEO_PATH = "/shortVideo/";
    public static final String ADVERT_PATH = "/advert/";
    public static final String SAVE_IMAGE_PATH = "/saveImage/";
    public static final String SAVE_MP4_PATH = "/saveMp4/";

    public static String getAppPath(String dirName) {
        String root = "";
        if (Build.VERSION.SDK_INT > 29) {
            root = ContextUtils.getCurApplication().getExternalFilesDir(null).getAbsolutePath() + "/hanxiang";
        } else {
            root = Environment.getExternalStorageDirectory().getPath() + "/hanxiang";
        }
        File appDir = new File(root, dirName);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        return appDir.getAbsolutePath();
    }

    public static void saveImageToGallery(Context activity, String filePath) {
        if(DoubleClickHelper.isOnDoubleClick())return;
        // 首先保存图片 创建文件夹
        File appDir = new File(getAppPath(SAVE_IMAGE_PATH));
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        //图片文件名称
        String fileName = "image_" + System.currentTimeMillis() + ".jpg";
        File mFile = new File(appDir, fileName);
        try {
            Bitmap bmp = BitmapFactory.decodeFile(filePath);
            FileOutputStream fos = new FileOutputStream(mFile);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        //把文件插入到系统图库
        String mFilePathName = mFile.getAbsolutePath();
        if (!TextUtils.isEmpty(mFilePathName) && mFile != null) {
            //通知图库更新
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(mFile);
            intent.setData(uri);
            activity.sendBroadcast(intent);
            HKToastUtils.showToast("图片已保存");
        }
    }
    public static void saveBitmapToGallery(Context activity, Bitmap bitmap) {
        // 首先保存图片 创建文件夹
        File appDir = new File(getAppPath(SAVE_IMAGE_PATH));
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        //图片文件名称
        String fileName = "image_" + System.currentTimeMillis() + ".jpg";
        File mFile = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(mFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        //把文件插入到系统图库
        String mFilePathName = mFile.getAbsolutePath();
        if (!TextUtils.isEmpty(mFilePathName) && mFile != null) {
            //通知图库更新
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(mFile);
            intent.setData(uri);
            activity.sendBroadcast(intent);
            HKToastUtils.showToast("图片已保存");
        }
    }
    public static File saveBitmapToTemp(Bitmap bitmap) {
        File appDir = new File(getAppPath(TEMP_PATH));
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = "temp_" + System.currentTimeMillis() + ".jpg";
        File mFile = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(mFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mFile;
    }
    public static void deleteTempFile() {
        File file = new File(StorageUtils.getAppPath(StorageUtils.TEMP_PATH));
        if (file.exists()) {
            if (file.isDirectory()) {
                File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
                for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
                    files[i].delete(); // 把每个文件 用这个方法进行迭代
                }
            }
        }
    }

    public static boolean tempDirIsExist() {
        File file = new File(StorageUtils.getAppPath(StorageUtils.TEMP_PATH));
        return (file.exists() && file.isDirectory() && file.list().length > 0);
    }
    public interface ISaveCallback{
        void saveSuccess();
    }
    /**保存图片到相册
     * @author: merbng
     * @time: 2023/1/14 15:45
     * @description:
     */
    public static void saveImageToGallery(Activity act, String filePath,ISaveCallback callback) {
        // 首先保存图片 创建文件夹
        File appDir = new File(getAppPath(SAVE_IMAGE_PATH));
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        //图片文件名称
        String fileName = "image_" + System.currentTimeMillis() + ".jpg";
        File mFile = new File(appDir, fileName);
        try {
            Bitmap bmp = BitmapFactory.decodeFile(filePath);
            FileOutputStream fos = new FileOutputStream(mFile);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        //把文件插入到系统图库
        String mFilePathName = mFile.getAbsolutePath();
        if (!TextUtils.isEmpty(mFilePathName) && mFile != null) {
            //通知图库更新
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(mFile);
            intent.setData(uri);
            act.sendBroadcast(intent);
            callback.saveSuccess();
        }
    }
}
